{% extends 'layout/basic.html' %}

{% block title %}register{% endblock %}

{% block css %}
    <style>
        .account {
            width: 500px;
            border: 1px solid #dddddd;
            border-radius: 10px;
            box-shadow: 10px 10px 10px #aaa;
            padding: 20px 40px;

            margin-left: auto;
            margin-right: auto;
            margin-top: 100px;

        }

        .account h1 {
            margin-top: 10px;
            text-align: center;
        }

        .error-msg {
            color: red;
            position: absolute;
            font-size: 13px;
        }

    </style>

{% endblock %}


{% block content %}
    <div class="account">
        <h1 style="text-align: center;">注册</h1>
        <form id="regform" method="POST" novalidate>
            {% csrf_token %}
            {% for field in form %}
                {% if field.name == 'code' %}
                    <div class="form-group">
                        <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                        <div class="row">
                            <div class="col-md-5">
                                {{ field }}
                                <span class="error-msg">{{ field.errors.0 }}</span>
                            </div>
                            <div class="col-md-5">
                                <input id="btnSms" type="button" class="btn btn-default" value="获取手机验证码">
                            </div>
                        </div>

                    </div>
                {% else %}
                    <div class="form-group">
                        <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                        {{ field }}
                        <span class="error-msg">{{ field.errors.0 }}</span>
                    </div>
                {% endif %}
            {% endfor %}
             <div class="row">
                <div class="col-md-4"></div>
                <div class="col-md-3">
                    <input id="btnSub" type="button" class="btn btn-primary" value="注 册">
                </div>
                <div class="col-md-2" style="margin-top: 10px;"><span class="glyphicon glyphicon-share-alt" aria-hidden="true"></span><a href="/web/login">返回</a></div>
            </div>

        </form>

    </div>
{% endblock %}

{% block js %}
    <script>
        $(function () {
            bindClickBtnSms();
            bindClickBtnSub();
        });

        function bindClickBtnSub() {
            $('#btnSub').click(function () {
                $('.error-msg').empty();
                $.ajax({
                    url: "/web/register/",
                    type: "POST",
                    data: $('#regform').serialize(),
                    dataType: "JSON",
                    success: function (res) {
                        if (res.status) {
                            location.href = res.data;
                        } else {
                            $.each(res.error, function (key, value) {
                                $("#id_" + key).next().text(value[0]);
                            })
                        }
                    }
                })
            })
        }

        function bindClickBtnSms() {
            $('#btnSms').click(function () {
                $('.error-msg').empty();
                var mobilePhone = $('#id_mobile_phone').val();

                $.ajax({
                    url: "/web/send/sms/",
                    type: "GET",
                    data: {mobile_phone: mobilePhone, tpl: "register"},
                    dataType: "JSON",
                    success: function (res) {
                        if (res.status) {
                            $('#btnSms').val('发送成功').prop('disabled', true);
                            var time = 59;
                            var event = setInterval(function () {
                                $('#btnSms').val(time + '秒后重新发送');
                                time = time - 1;
                                if (time == 0) {
                                    clearInterval(event);
                                    $('#btnSms').val('获取手机验证码').prop('disabled', false);
                                }
                            }, 1000)
                        } else {
                            $.each(res.error, function (key, value) {
                                $("#id_" + key).next().text(value[0]);
                            })
                        }
                    }
                })
            })
        }


    </script>

{% endblock %}